package xyz.robinbingo.leetcode;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int x) {
        val = x;
    }

    public ListNode(int[] ints) {
        val = ints[0];
        if (ints.length > 1) {
            setNext(this, ints, 1);
        }
    }

    private void setNext(ListNode node, int[] ints, int i) {
        if (i == ints.length) {
            return;
        }
        node.next = new ListNode(ints[i]);
        setNext(node.next, ints, i + 1);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sout(this, sb);
        return sb.toString();
    }

    private void sout(ListNode node, StringBuilder sb) {
        if (node == null) {
            return;
        }
        sb.append(node.val).append(",");
        sout(node.next, sb);
    }
}